import requests,json
class ApiRequest:
    def get(self,url,headers=None,data=None):
        if headers is not None:
            res=requests.get(url=url,headers=headers,data=data)
        else:
            res=requests.get(url=url)
        return res.json()
    def post(self,url,headers,data):
        if headers is not None:
            res=requests.post(url=url,headers=headers,data=data)
        else:
            res=requests.post(url=url,data=data)
        if str(res.status_code)=="200":
            return res.json()
        else:
            return res.text
    def put(self,url,headers=None,data=None):
        if headers is not None:
            res=requests.put(url=url,headers=headers,data=data)
        else:
            res=requests.put(url=url)
        return res.json()
    def delete(self,url,headers=None,data=None):
        if headers is not None:
            res=requests.delete(url=url,headers=headers,data=data)
        else:
            res=requests.delete(url=url)
        return res.json()
    #第一种方式
    def all_method(self,method,url,headers=None,data=None):
        if method.lower()=='get':
            res= self.get(url,headers=headers,data=data)
        elif method.lower()=='post':
            res= self.post(url,headers,data)
        elif method.lower()=='put':
            res= self.put(url,headers=headers,data=data)
        elif method.lower()=='delete':
            res= self.delete(url,headers=headers,data=data)
        else:
            res="请求方法不正确"
        return json.dumps(res, ensure_ascii=False, indent=4, sort_keys=True, separators=(',',':'))
    #第二种方式
    def send_request(self,method,url,params=None,data=None,headers=None,cookies=None,files=None,auth=None,
                     timeout=None,allow_redirects=None,proxies=None,hooks=None,stream=None,verify=None,json=None):
        self.res=requests.request(method=method,url=url,params=params,data=data,headers=headers,
                                  cookies=cookies,files=files,auth=auth,timeout=timeout,allow_redirects=allow_redirects,
                                  proxies=proxies,hooks=hooks,stream=stream,verify=verify,json=json)
        return self.res
